<template>
  <canvas
    ref="canvas"
    :style="{ width: width + 'px', height: height + 'px' }"
    :size="200"
  ></canvas>
</template>

<script>
import QRCode from "qrcode";

export default {
  name: "QrCode",
  props: {
    value: {
      type: String,
      required: true,
    },
    width: {
      type: Number,
      default: 200,
    },
    height: {
      type: Number,
      default: 200,
    },
  },
  mounted() {
    this.renderQrCode();
  },
  watch: {
    value() {
      this.renderQrCode();
    },
  },
  methods: {
    renderQrCode() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext("2d");
      QRCode.toCanvas(canvas, this.value, (error) => {
        if (error) console.error(error);
      });
    },
  },
};
</script>
<style>
.qrCode {
  width: 200px !important;
  height: 200px !important;
}
</style>
